import type { SysApi, LoginUserInfo, LoginUserToken } from "./types";
import { useUserStore } from "@/stores/modules/user";

class SysApiImpl implements SysApi {
  userStore: any;

  constructor() {
    this.userStore = useUserStore();
  }

  /** 检查用户是否登录. */
  checkLogin(): boolean {
    return !this.userStore.isTokenExpires();
  }
  /** 获取登录用户信息. */
  userInfo(): LoginUserInfo {
    return this.userStore.user;
  }
  /** 检查是否包含该角色 */
  hasRole(roleCode: string): boolean {
    const userInfo = this.userInfo();
    return (
      userInfo &&
      userInfo.roleCodes &&
      userInfo.roleCodes.length > 0 &&
      userInfo.roleCodes.includes(roleCode)
    );
  }
  /** 检查是否包含该权限 */
  hasPerm(permCode: string): boolean {
    const userInfo = this.userInfo();
    return (
      userInfo &&
      userInfo.permCodes &&
      userInfo.permCodes.length > 0 &&
      userInfo.permCodes.includes(permCode)
    );
  }
  /** 获取登录用户Token信息. */
  userToken(): LoginUserToken {
    return this.userStore.userToken;
  }
}

export { SysApiImpl };
